<!--
 * Date: 2018年01月02日 09:32:33
 * LastEditors: lvyou
 * LastEditTime: 2019年12月03日 16:47:36
 * FilePath: \youlinweb-master\src\pages\shuohsuo\publishshuoshuo\publishuoshuo.vue
 * copyright(c) 2019-2025 版权为浙江双城网络科技集团有限公司所有，违者必究。
 -->
<template>
  <div>
    <ylheader @save="savecaogao" @send='sendcomment' :lefttext="true" :lefttexts="'取消'" :title="'发评论'" :righttext="true" :righttexts="'发送'"></ylheader>
    <div>
    <div class="loadmore-container" ref="containerWrapper">
    <div class="loadmore-ul">
      <publishshuoshuo :textareatexts="content" :thumbnailslist="pics"  @haha="textareatext" @commentpics="uploadpics" @changeTextArea="changeTextArea"></publishshuoshuo>
    </div>
    </div>
    </div>
  </div>
</template>
<script>
import ylheader from "src/components/yl-header.vue";
import publishshuoshuo from "src/components/addcomments.vue";
import api from "src/api/index";
export default {
  name:"publishshuoshuooo",
  data() {
    return {
      content: "",
      pics: [],
      uploadpictures: [],
      picstring: [],
      caogaoId: "",
      doubleclick: false
    };
  },
  mounted() {
    this.initLooksaycaogao().then(data => {
      this.initScroll();
    });
  },
  components: {
    ylheader,
    publishshuoshuo
  },
  methods: {
    initLooksaycaogao() {
      let p = new Promise((resolve, reject) => {
        const data = {
          saysId: this.$route.query.saysId
        };
        /**
               * 查询评论草稿
               */
        api.Querylooksaycaogao(data).then(res => {
          if (res.success) {
            if (res.data) {
              this.pics = res.data.picture;
              this.content = res.data.content;
              this.caogaoId = res.data.id;
            }
          }
          resolve("获取草稿数据...");
        });
      });
      return p;
    },
    //初始化scroll
    initScroll() {
      let p = new Promise((resolve, reject) => {
        this.$nextTick(() => {
          let containerWrapper = this.$refs.containerWrapper;
          this.setScroll(containerWrapper, this);
        });
        resolve("初始化scroll结束...");
      });
      return p;
    },
    setScroll(containerWrapper, _this) {
      localStorage.tempPageHeight = document.getElementsByClassName(
        "loadmore-ul"
      )[0].scrollHeight;
      _this.myscroll = new iScroll(containerWrapper, {
        //上拉时触发
        onScrollMove: function() {
          if (
            this.y <= this.wrapperH - this.scroller.clientHeight - 50 &&
            _this.is_r == false
          ) {
            //加载数据
            _this.is_r = true;
          }
        },
        onScrollEnd: function() {},
        onScrollStart: function() {
          if (
            localStorage.tempPageHeight !=
            document.getElementsByClassName("loadmore-ul")[0].scrollHeight
          ) {
            this.refresh();
            localStorage.tempPageHeight = document.getElementsByClassName(
              "loadmore-ul"
            )[0].scrollHeight;
          }
        }
      });
    },
    textareatext(data) {
      this.content = data;
    },
    uploadpics(data) {
      this.uploadpictures = data;
    },
    savecaogao() {
      if (this.doubleclick) {
        return;
      }
      this.doubleclick = true;

      this.uploadpictures = [...this.pics];
      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");
      if (!this.content && !this.picstring) {
          this.doubleclick = false;
        this.$router.go(-1);
        return;
      }
      const myparams = {
        saysId: this.$route.query.saysId,
        content: this.content,
        draft: 1,
        pictureJson: this.picstring,
        saysUserId: this.$route.query.saysUserId,
        parentId: this.$route.query.parentId,
        replyId: this.$route.query.replyId,
        id: this.caogaoId
      };
      api.Queryshuoshuocommentdraf(myparams).then(res => {
        if (res.success) {
          this.$router.go(-1);
        } else {
          this.doubleclick = false;
          this.$toast(res.msg);
        }
      }).catch(error=>{
          this.doubleclick = false;
      });
;
    },
    sendcomment() {
      if (this.doubleclick) {
        return;
      }
      this.doubleclick = true;
      this.uploadpictures = [...this.pics];
      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");

      if (!this.content && !this.picstring) {
        this.$toast("请编写评论");
        this.doubleclick = false;
        return;
      }
      //  this.content = this.$util.getTextUrl(this.content);//识别链接
      const comment = {
        saysId: this.$route.query.saysId,
        content: this.content,
        draft: 0,
        pictureJson: this.picstring,
        saysUserId: this.$route.query.saysUserId,
        parentId: this.$route.query.parentId,
        replyId: this.$route.query.replyId,
        id: this.caogaoId
      };
      api.Queryshuoshuocommentdraf(comment).then(res => {
        if (res.success) {
          this.$util.saveFootPrint({actType:4,typeValue:res.value});//用户轨迹
          if (typeof this.$route.query.shuoshuoIndex != "undefined"&&localStorage.sayStorage) {
            let sayObj = JSON.parse(localStorage.sayStorage);
            sayObj.commentlist[this.$route.query.shuoshuoIndex].commentNumber++;
            let tempSayObj = JSON.stringify({
              commentlist: sayObj.commentlist,
              totalPage: sayObj.totalPage,
              hasmore: sayObj.hasmore,
              pageone: sayObj.pageone,
              pagetwo: sayObj.pagetwo,
              mypage: sayObj.mypage,
              timeorhot: sayObj.timeorhot
            });
            localStorage.sayStorage = tempSayObj;
          }
          this.$router.go(-1);
        } else {
          this.doubleclick = false;
          this.$toast(res.msg);
        }
      });
    },
    changeTextArea() {
      this.$nextTick(() => {
        this.myscroll.refresh();
      });
    }
  }
};
</script>
<style lang="less" scoped>
.loadmore-container {
  height: 95vh;
  overflow: hidden;
  margin: 0 auto;
}
.loadmore-ul {
  padding-bottom: 0.2rem;
  list-style: none;
}
</style>